﻿using Dapper;
using log4net;
using StoreProject.DBUtil;
using StoreProject.Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StoreProject.DAL
{
    public class BoxNumDAL
    {
        ILog _log = LogManager.GetLogger("BoxNumDAL");

        public BoxNumDAL()
        {

        }
        public IDbConnection Conn
        {
            get
            {
                return MySqlHelper.Conn;
            }
        }

        public int? Insert(BoxNumEntity model)
        {
            try
            {
                StringBuilder sql = new StringBuilder("INSERT INTO Boxnum(Id,OrderId,LineType,FinishedProductName,Batch,Operator,CreateTime,JuanNum,MaoZhong,OneBoxCount,Store,AllCount,BoxNum)");
                sql.Append(" VALUES( @Id,@OrderId,@LineType,@FinishedProductName,@Batch,@Operator,@CreateTime,@JuanNum,@MaoZhong,@OneBoxCount,@Store,@AllCount,@BoxNum)");
                using (Conn)
                {
                    int insert_Result = this.Conn.Execute(sql.ToString(), model);
                    return insert_Result;
                }
            }
            catch (Exception ex)
            {
                _log.Error("Insert", ex);
                return 0;
            }
        }

        public int Update(BoxNumEntity model)
        {
            try
            {
                using (this.Conn)
                {
                    int update_Result = this.Conn.Update(model);
                    return update_Result;
                }
            }
            catch (Exception ex)
            {
                _log.Error("Update", ex);
                return 0;
            }
        }

        public BoxNumEntity Get(string id)
        {
            BoxNumEntity model = null;
            try
            {
                using (this.Conn)
                {
                    model = this.Conn.Get<BoxNumEntity>(id);
                }
            }
            catch (Exception ex)
            {
                _log.Error("Get", ex);
            }
            return model;
        }

        public bool IsExists(string id)
        {
            bool isExist = false;
            using (this.Conn)
            {
                BoxNumEntity model = Get(id);
                isExist = (model == null) ? false : true;
            }
            return isExist;
        }

        public int Delete(string id)
        {
            using (Conn)
            {
                BoxNumEntity model = new BoxNumEntity();
                model.Id = id;
                return Conn.Delete(model);
            }
        }

        public int Delete(BoxNumEntity model)
        {
            using (Conn)
            {
                return Conn.Delete(model);
            }
        }

        public IEnumerable<BoxNumEntity> GetList()
        {
            using (Conn)
            {
                return Conn.GetList<BoxNumEntity>();
            }
        }

        public IEnumerable<BoxNumEntity> GetListEx()
        {
            using (Conn)
            {
                string sql = "select * from Boxnum";
                return Conn.Query<BoxNumEntity>(sql);
            }
        }

        public IEnumerable<BoxNumEntity> GetListByWhere(string strWhere)
        {
            using (Conn)
            {
                string sql = "select * from Boxnum";
                if (!string.IsNullOrEmpty(strWhere))
                {
                    sql += " where " + strWhere;
                }
                return Conn.Query<BoxNumEntity>(sql);
            }
        }


        public int DeleteAll()
        {
            using (Conn)
            {
                string sql = "delete from Boxnum";
                return Conn.Execute(sql);
            }
        }
        public BoxNumEntity GetById(string id)
        {
            try
            {
                using (Conn)
                {
                    string sql = "select * from Boxnum where id=@id order by id desc ";
                    return Conn.QueryFirstOrDefault<BoxNumEntity>(sql, new { id = id });
                }
            }
            catch (Exception ex)
            {
                _log.Error("GetById", ex);
                return null;
            }
        }
    }
}
